﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZhimeiSoft.Framework.Client.Base;
using ZhimeiSoft.Framework.Common.RefectUtils;
using ZhimeiSoft.CharmManager.Client.Forms.Utils;
using System.Reflection;


namespace ZhimeiSoft.CharmManager.Client.Forms
{
    public partial class frmMain : MainFormBase
    {
        private BaseUserControl currentContent;

        private void InitStatusBar()
        {
            StringBuilder sbDate = new StringBuilder();
            sbDate.Append("当前日期：");
            sbDate.Append(DateTime.Now.ToString("yyyy-MM-dd"));
            sbDate.Append(",星期");
            switch(DateTime.Now.DayOfWeek)
            {
                case DayOfWeek.Monday:
                    sbDate.Append("一");
                    break;
                case DayOfWeek.Tuesday:
                    sbDate.Append("二");
                    break;
                case DayOfWeek.Wednesday:
                    sbDate.Append("三");
                    break;
                case DayOfWeek.Thursday:
                    sbDate.Append("四");
                    break;
                case DayOfWeek.Friday:
                    sbDate.Append("五");
                    break;
                case DayOfWeek.Saturday:
                    sbDate.Append("六");
                    break;
                case DayOfWeek.Sunday:
                    sbDate.Append("日");
                    break;
            }
            tssLInfo.Text = sbDate.ToString();

        }

        public frmMain()
        {
            InitializeComponent();
            //this.CreateConetent(this, new DefaultUserControl());

            InitStatusBar();

            string configFile = Path.Combine(Application.StartupPath, "CharmConfig.xml");
            List<ToolEntity> tools = new ToolAppConfig(configFile).Tools;
            foreach (ToolEntity tool in tools)
            {
                ToolStripButton button = new ToolStripButton();
                button.Image = this.GetImageFromFile(tool.Image);
                button.ImageAlign = ContentAlignment.TopCenter;
                button.ImageScaling = ToolStripItemImageScaling.None;
                button.ImageTransparentColor = System.Drawing.Color.Magenta;
                if (!string.IsNullOrEmpty(tool.Name))
                {
                    button.Name = tool.Name + Guid.NewGuid().ToString();
                }
                button.Size = new Size(128, 128);
                if (!string.IsNullOrEmpty(tool.Hint))
                {
                    button.ToolTipText = tool.Hint;
                }
                button.Text = tool.Title;
                button.TextAlign = ContentAlignment.BottomCenter;
                button.TextImageRelation = TextImageRelation.ImageAboveText;
                this.tsMain.Items.Add(button);
                
                button.Click += new EventHandler(button_Click);
                button.Tag = tool.Url;
            }

            //第一个作为默认启动的控件
            ToolStripButton item = (ToolStripButton)this.tsMain.Items[0];
            button_Click(item, null);
        }

        void button_Click(object sender, EventArgs e)
        {
            try
            {
                ToolStripButton button = sender as ToolStripButton;
                if (button != null)
                {
                    string type = button.Tag as string;
                    if (!string.IsNullOrEmpty(type))
                    {
                        string asmName = string.Empty;
                        string typeName = string.Empty;
                        TypeHelper.ParseType(type, out typeName, out asmName);
                        Assembly asm = typeof(BaseUserControl).Assembly;
                        object[] ctorParams = new object[] { this.zmApp };
                        BaseUserControl control = ReflectHelper.Instance.CreateObject(asm, asmName, typeName, ctorParams) as BaseUserControl;
                        if (control != null)
                        {
                            this.CreateConetent(button, control);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void CreateConetent(object sender, BaseUserControl control)
        {
            /*if (this.plContent1.Tag == sender)
                return;

            if (this.currentContent != null)
            {
                this.plContent1.Controls.Remove(this.currentContent);
            }
            this.currentContent = control;
            this.currentContent.Dock = DockStyle.Fill;
            this.plContent1.Controls.Add(this.currentContent);
            this.plContent1.Tag = sender;*/
            if (this.plContent.Tag == sender)
                return;

            if (this.currentContent != null)
            {
                this.plContent.Controls.Remove(this.currentContent);
            }
            this.currentContent = control;
            this.currentContent.Dock = DockStyle.Fill;
            this.plContent.Controls.Add(this.currentContent);
            this.plContent.Tag = sender;
        }

        private Image GetImageFromFile(string fileName)
        {
            if (string.IsNullOrEmpty(fileName))
                return null;

            string file = Path.Combine(System.Environment.CurrentDirectory, fileName);
            try
            {
                return Image.FromFile(file);
            }
            catch 
            {
                return null;
            }

        }

    }
}
